package com.kye.express.business.customer_intro.history;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.customer_intro.add.CustomerIntroAddActivity;
import com.kye.express.business.customer_intro.detail.CustomerIntroDetailActivity;
import com.kye.express.business.waybill_take_photo.history.WaybillTakePhotoHistoryActivityContract;
import com.kye.express.databinding.ItemCustomerIntroHistoryBinding;
import com.kye.express.databinding.ItemWaybillTakePhotoHistoryBinding;
import com.kye.model.bean.customer_intro.CustomerIntroHistoryResultBean;
import com.kye.model.bean.waybill_take_photo.QueryWaybillTakePhotoHistoryResultBean;

import io.reactivex.functions.Consumer;

class CustomerIntroHistoryListItemViewBinder extends me.drakeet.multitype.ItemViewBinder<CustomerIntroHistoryResultBean.DatasBean, CustomerIntroHistoryListItemViewBinder.ViewHolder> {

    private Context mContext;

    public CustomerIntroHistoryListItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_customer_intro_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CustomerIntroHistoryResultBean.DatasBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private CustomerIntroHistoryResultBean.DatasBean data;
        ItemCustomerIntroHistoryBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(CustomerIntroHistoryResultBean.DatasBean item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                dataBinding.btnEdit.setVisibility(data.getEditable() == 1 ? View.VISIBLE : View.GONE);

                RxView.clicks(dataBinding.btnViewPhone)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                dataBinding.tvPhone.setText(data.getScTel());
                            }
                        });

                RxView.clicks(dataBinding.btnEdit)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                CustomerIntroAddActivity.startActivity(mContext, data.getSysGuid(), TextUtils.isEmpty(data.getScry()));
                            }
                        });
                RxView.clicks(dataBinding.getRoot())
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {

                            @Override
                            public void accept(Object o) throws Exception {
                                CustomerIntroDetailActivity.startActivity(mContext, data);
                            }
                        });
            }
        }
    }
}
